package com.zhike.b2b.search;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReadFile
{
  static	Logger log = LoggerFactory.getLogger(ReadFile.class);
	
  public static String readExcel(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    try
    {
      FileInputStream localFileInputStream = new FileInputStream(paramString);
      Workbook localWorkbook = Workbook.getWorkbook(localFileInputStream);
      localWorkbook.getNumberOfSheets();
      Sheet[] arrayOfSheet = localWorkbook.getSheets();
      for (int i = 0; i < arrayOfSheet.length; i++)
      {
        Sheet localSheet = localWorkbook.getSheet(i);
        int j = localSheet.getRows();
        int k = localSheet.getColumns();
        for (int m = 0; m < k; m++)
          for (int n = 0; n < j; n++)
            localStringBuffer.append(localSheet.getCell(m, n).getContents());
      }
      localWorkbook.close();
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return localStringBuffer.toString();
  }

/*  public static String readPdf(String paramString)
    throws Exception
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    FileInputStream localFileInputStream = new FileInputStream(paramString);
    PDFParser localPDFParser = new PDFParser(localFileInputStream);
    localPDFParser.parse();
    PDFTextStripper localPDFTextStripper = new PDFTextStripper();
    localStringBuffer.append(localPDFTextStripper.getText(localPDFParser.getPDDocument()));
    localFileInputStream.close();
    return localStringBuffer.toString();
  }
*/
  public static String readTxt(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    try
    {
      FileReader localFileReader = new FileReader(paramString);
      BufferedReader localBufferedReader = new BufferedReader(localFileReader);
      String str = null;
      while ((str = localBufferedReader.readLine()) != null)
        localStringBuffer.append(str + "\r");
      localBufferedReader.close();
      localFileReader.close();
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
    }
    return localStringBuffer.toString().trim();
  }

  public static String readWord(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    try
    {
      HWPFDocument localHWPFDocument = new HWPFDocument(new FileInputStream(paramString));
      Range localRange = localHWPFDocument.getRange();
      int i = localRange.numParagraphs();
      for (int j = 0; j < i; j++)
      {
        Paragraph localParagraph = localRange.getParagraph(j);
        localStringBuffer.append(localParagraph.text());
      }
    }
    catch (Exception localException)
    {
    }
    return localStringBuffer.toString().trim();
  }

  public static String readPpt(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    try
    {
      SlideShow localSlideShow = new SlideShow(new HSLFSlideShow("F:\\file\\ppt\\09-11-14.ppt"));
      Slide[] arrayOfSlide = localSlideShow.getSlides();
      for (int i = 0; i < arrayOfSlide.length; i++)
      {
        TextRun[] arrayOfTextRun = arrayOfSlide[i].getTextRuns();
        for (int j = 0; j < arrayOfTextRun.length; j++)
          localStringBuffer.append(arrayOfTextRun[j].getText());
        localStringBuffer.append(arrayOfSlide[i].getTitle());
      }
    }
    catch (Exception ex)
    {
      log.error(ex.getStackTrace().toString());
    }
    return localStringBuffer.toString();
  }
}

